import { WereWolfMainState } from "@/werewolf/mainState/state";
import { WereWolfModule } from "@/werewolf/module";
import { GameMode, Player } from "@minecraft/server";
import { SAPIGameConfig } from "@sapi-game/config";
import { Dimensions } from "@sapi-game/constants";
import { AutoStopComponent } from "@sapi-game/gameComponent/common/autoStop";
import { PlayerGroupSet } from "@sapi-game/gamePlayer/groupSet";
import { Game, TeamScoreBoard } from "@sapi-game/main";
import { BuildWereWolfIdOptions, BuildWereWolfPlayerName } from "../func";
import { WereWolfPlayer } from "../player";
import { WereWolfNoPicker } from "./noPicker";
import { WereWolfSettingComponent } from "./setting";

export class WereWolfPickNumberState extends WereWolfModule.State {
    override onEnter(): void {
        this.init();
        this.addComponent(AutoStopComponent<WereWolfPlayer>, {
            groupSet: new PlayerGroupSet([this.context.players]),
            onStopGame: () => {
                this.engine.stopGame();
            },
            onLeave: (p) => {
                p.no = 0;
                this.context.players.delete(p);
            },
            immediateDie: true,
        });
        this.addComponent(WereWolfNoPicker, {
            players: this.context.players,
        }).addComponent(WereWolfSettingComponent);

        //右侧计分板显示
        this.addComponent(
            TeamScoreBoard<WereWolfPlayer>,
            BuildWereWolfIdOptions(this.context.players)
        );
        this.getComponent(TeamScoreBoard).show();

        this.addComponents(this.context.map.pickNoView);
    }

    init() {
        //所有空闲玩家入组
        const freePlayers = Game.playerManager.getFreePlayers();
        freePlayers.forEach((p) => this.join(p));
    }

    /**玩家加入游戏 */
    join(player: Player) {
        const gamePlayer = this.playerManager.get(player);
        if (!gamePlayer.player) return;
        //初始化玩家
        gamePlayer.player.setGameMode(GameMode.Adventure);
        gamePlayer.player.setSpawnPoint({
            dimension: Dimensions.Overworld,
            ...this.context.map.watingRegion.center,
        });
        gamePlayer.clear();
        gamePlayer.no = 0;
        gamePlayer.player.teleport(this.context.map.watingRegion.center);
        //如果不在组，就加入
        if (!this.context.players.has(gamePlayer))
            this.context.players.add(gamePlayer);
        gamePlayer.runCommand("function werewolf/noPicker");
    }

    /**检查玩家是否足够 */
    checkPlayers() {
        const size = this.context.players.filter(
            (p) => p.isValid && p.no != 0
        ).length;
        if ((!SAPIGameConfig.config.debugMode && size < 3) || size < 1) {
            return false;
        }
        return true;
    }

    start() {
        if (!this.checkPlayers()) {
            this.context.players.title("人数过少，无法开始");
            return false;
        }
        this.context.players.removeWhere((p) => !p.isValid || p.no === 0);
        this.transitionTo(WereWolfMainState);
        return true;
    }
}
